from event_rule.dwc import NO_BET_AMOUNT_GAME_TYPE, NO_WIN_AMOUNT_GAME_TYPE

ACTIVE_USER_ANALYSIS_CONFIG = {
    'real_time_report': False,
    'group_by': '_user_id',
    'interval_type_list': ['day'],
    'target_event_id': ['recharge', 'withdraw', 'bet_order', 'platform_award', 'withdraw_apply', 'withdraw_return'],
    'values': [
        {
            "value_id": "active_days",
            "target_value": "_active_days",
            "value_name": "投注天数",
            "filter": [
                {"term": {"_event_id": "bet_order"}}
            ],
            "operation": "cardinality",
            "value_type": "long",
            "description": "时间段用户参与投注的天数"
        },
        {
            "value_id": "recharge_count",
            "target_value": "recharge_price",
            "value_name": "充值次数",
            "filter": [
                {'terms': {'_event_id': ['recharge']}},
            ],
            "operation": "value_count",
            "value_type": "long"
        },
        {
            "value_id": "recharge",
            "target_value": "recharge_price",
            "value_name": "充值金额",
            "filter": [
                {'terms': {'_event_id': ['recharge']}},
            ],
            "value_type": "float",
            "adjust_amount": True,
        },
        {
            "value_id": "withdraw_count",
            "target_value": "withdraw_real_price",
            "value_name": "提现次数",
            "filter": [
                {'term': {'_event_id': 'withdraw'}}
            ],
            "operation": "value_count",
            "value_type": "long",
        },
        {
            "value_id": "withdraw",
            "target_value": "withdraw_real_price",
            "value_name": "提现金额",
            "filter": [
                {'term': {'_event_id': 'withdraw'}}
            ],
            "value_type": "float",
            "adjust_amount": True,
        },
        {
            "value_id": "real_profit",
            "value_name": "充提盈利",
            "value_expr": "withdraw - recharge",
            "adjust_amount": True,
            "description": "时间段累计充值 - 累计提现"
        },
        {
            "value_id": "_total_bet",
            "target_value": 'bet_order_bet_amount',
            "value_name": "投注金额",
            "filter": [
                {'term': {'_event_id': 'bet_order'}},
            ],
            "hide_result": True,
            "value_type": "float",
            "adjust_amount": True,
        },
        {
            "value_id": "total_bet",
            "value_name": "投注金额",
            "value_expr": "_total_bet - lose_award_amount",
            "adjust_amount": True
        },
        {
            "value_id": "lose_award_amount",
            "target_value": "bet_order_award_amount",
            "value_name": "负扣金额",
            "filter": [
                {"terms": {"bet_order_sub_type.keyword": NO_BET_AMOUNT_GAME_TYPE}},
                {"range": {"bet_order_award_amount": {"lt": 0}}}
            ],
            "value_type": "float",
            "hide_result": True,
            "adjust_amount": True,
        },
        {
            "value_id": "invalid_bet_amount",
            "target_value": "bet_order_bet_amount",
            "value_name": "无效投注",
            "filter": [
                {"terms": {"bet_order_sub_type.keyword": NO_WIN_AMOUNT_GAME_TYPE}},
                {"range": {"bet_order_award_amount": {"gt": 0}}}
            ],
            "value_type": "float",
            "hide_result": True,
            "adjust_amount": True,
        },
        {
            "value_id": "valid_bet_amount",
            "value_name": "有效投注",
            "value_expr": "_total_bet - invalid_bet_amount + (lose_award_amount * -1)",
            "adjust_amount": True,
        },
        {
            "value_id": "total_tax_amount",
            "target_value": "bet_order_tax_amount",
            "value_name": "税收金额",
            "filter": [
                {"term": {"_event_id": "bet_order"}}
            ],
            "value_type": "float",
            "adjust_amount": True,
        },
        {
            "value_id": "total_profit",
            "target_value": ['bet_order_award_amount', '-', 'bet_order_bet_amount'],
            "value_name": "玩法盈利",
            "filter": [
                {'term': {'_event_id': 'bet_order'}},
            ],
            "value_type": "float",
            "adjust_amount": True,
            "description": "时间段用户中奖金额 - 投注金额"
        },
        {
            "value_id": "total_platform_award_amount",
            "target_value": "platform_award_amount",
            "value_name": "系统奖励",
            "filter": [
                {"term": {"_event_id": "platform_award"}}
            ],
            "value_type": "float",
            "adjust_amount": True,
            "description": "时间段用户累计奖励金额"
        },
        {
            "value_id": "profit_loss_difference",
            "value_name": "盈亏差值",
            "value_expr": "real_profit - total_profit - total_platform_award_amount",
            "value_type": "float",
            "adjust_amount": True,
            "description": "充提盈利-玩法盈利-系统奖励"
        },
        {
            "value_id": "last_active_channel",
            "target_value": "_chn",
            "value_name": "最后活跃渠道",
            "filter": [
                {"term": {"_event_id": "active"}}
            ],
            "sort": [
                {"_event_time": {"order": "desc"}}
            ],
            "operation": "top_hits"
        },
    ]
}
